@<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
        <!-- 添加角色 -->
        <el-row>
            <el-col>
                <el-button type="primary">添加角色</el-button>
            </el-col>
        </el-row>
        <!-- 渲染角色列表 -->
        <el-table class="roles-table" :data="rolesList" stripe border>
            <!-- 展开列 -->
            <el-table-column type="expand">
                <template slot-scope="scope">

                    <!-- 栅格布局 -->
                    <el-row  class="row-border vcenter" v-for="(roel,index) in scope.row.children" :key="index">
                       
                        <!-- 渲染一级权限 -->
                        <el-col :span="6">
                            <el-tag @close="removeRoleById(scope.row, roel.id)" type="primary" closable >{{roel.authName}} </el-tag> 
                            <i class="el-icon-caret-right"></i>
                        </el-col>
                        <!-- 渲染二级和三级权限 -->
                        <el-col :span="18">
                            <el-row class="vcenter" v-for="croel in roel.children" :key="croel.id">
                                <!-- 二级权限 -->
                                <el-col :span="6" >
                                    <el-tag @close="removeRoleById(scope.row, croel.id)" type="success" closable>{{croel.authName}}</el-tag>
                                    <i class="el-icon-caret-right"></i>
                                </el-col>
                                <!-- 三级权限 -->
                                <el-col :span="18">
                                    <el-tag @close="removeRoleById(scope.row, ccroel.id)" type="warning" closable 
                                    v-for="ccroel in croel.children" :key="ccroel.id">{{ccroel.authName}}</el-tag>
                                </el-col>
                            </el-row>
                        </el-col>
                    </el-row>

                </template>
            </el-table-column>

            <!-- 索引列 -->
            <el-table-column type="index"></el-table-column>
            <el-table-column prop="roleName" label="角色名称"></el-table-column>
            <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
            <el-table-column  label="操作">
                <el-row slot-scope="scope">
                    <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
                    <el-button type="danger" size="mini" icon="el-icon-delete" >删除</el-button>
                    <el-button @click="setRoles(scope.row)" type="warning" size="mini" icon="el-icon-setting">分配权限</el-button>
                </el-row>
            </el-table-column>
        </el-table>
    </el-card>

    <!-- 权限树对话框 -->
    <el-dialog
    title="权限管理"
    :visible.sync="setRolesDialogVisible"
    width="50%"
    >
    <!-- 权限树 组件 -->
    <el-tree :data="rolesTree" :props="treeProps" show-checkbox node-key="id"
    default-expand-all :default-checked-keys="defaultKeys" ref="treeRef"
    ></el-tree>
    <span slot="footer" class="dialog-footer">
    <el-button @click="setRolesDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="allotRights">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
export default {
    data() {
        return {
            // 角色列表
            rolesList: [],

            //权限树是否展示
            setRolesDialogVisible: false, 
            
            // 所有权限数据
            rolesTree: [],

            // 树形控件 数据绑定对象
            treeProps: {
                // 嵌套属性
                children: 'children',
                // 展示的值
                label: 'authName'
            },
            // 树形控件 默认选中的id值数组
            defaultKeys: [],

            // 修改权限的角色id
            roleId: '',
        }
    },
    created() {
        this.getRolesList()
    },
    methods: {
        // 获取角色列表
        async getRolesList() {
            const {data: res} = await this.$http.get('roles')
            if(res.meta.status != 200) return this.$message.error("获取角色列表失败")
            this.rolesList = res.data
        },
        //移除权限
        async removeRoleById(role, rightsId) {
            const roleId = role.id;
            console.log(roleId)
            console.log(rightsId)
            const result = await this.$confirm('确定删除该权限吗?',{
                confirmButton: '确认',
                cancelButton: '取消',
                type: 'warning'
            }).catch(err => err)
            if (result != 'confirm') return this.$message.info('取消删除')

            // 发起删除请求                               `roles/:roleId/rights/:rightId`
            const {data: res} = await this.$http.delete(`roles/${roleId}/rights/${rightsId}`)

            //获取最新的权限数据
            role.children = res.data;
            this.$message.success('删除成功')
        },
        // 展示分配权限对话框
        async setRoles(node) {

            // 保存id,修改权限时使用
            this.roleId = node.id
            // 提前清空defaultKeys
            this.defaultKeys = []

            

            // 递归获取三级节点ID
            this.getRolesId(node, this.defaultKeys)
            // 获取权限列表
            const {data: res} = await this.$http.get(`rights/tree`)
            if(res.meta.status != 200) return this.$message.error("获取数据失败")

            // 保存权限数据
            this.rolesTree = res.data
            this.$message.success('获取成功')

            // 展示对话框
            this.setRolesDialogVisible = true

        },
        // 递归获取权限id
        getRolesId(node, array) {
            // 如果是三级节点
            if(!node.children) {
              return array.push(node.id)
            }
            node.children.forEach(item => {
                this.getRolesId(item,array)
            })
        },
        // 点击分配权限
        async allotRights() {
            // 权限id数组
            const keys = [
                ...this.$refs.treeRef.getCheckedKeys(),
                ...this.$refs.treeRef.getHalfCheckedKeys()
            ]
            console.log(keys)
            //转换为字符串
            const idStr = keys.join(',');

            //发起角色授权请求
            const {data: res } = await this.$http.post(`roles/${this.roleId}/rights`,{
                rids: idStr
            })
            if(res.meta.status != 200) {
                return this.$message.error("授权失败")
            }
            this.$message.success("授权成功")
            
            this.getRolesList()
            this.setRolesDialogVisible = false;
            
            
        }
    }

}
</script>

<style lang="less" scope>
.roles-table {
    margin-top: 15px;
    .el-row{
    width: 270px;
    }

    .el-tag {
        margin: 7px;
       
    }
    .row-border {
        width: 100%;
        border-bottom: 1px solid #eee;
        .el-row {
            width: 100%;
            border-bottom: 1px solid #eee;
        }
        .el-row:last-of-type {
            width: 100%;
            border-bottom: 0;
        }
    }
    .row-border:nth-of-type(1){
        border-top: 1px solid #eee;
    }
}

.vcenter {
    display: flex;
    align-items: center;
}

</style>